package service;

import entity.BusEntity;
import entity.UserBusDto;
import entity.UserBusMappingEntity;
import frontEnd.Web;
import mapper.BusMapper;
import mapper.UserBusMappingMapper;
import util.CommonUtil;

import java.util.List;
import java.util.Scanner;

/**
 * @author HuangJH
 * @create 2023-11-30-17:50
 */
public class BusService {

    private BusMapper busMapper = new BusMapper();
    private UserBusMappingMapper userBusMappingMapper = new UserBusMappingMapper();

    /**
     * 操作班车相关业务
     * @param selector
     * @return
     */
    public boolean operateBus(int selector) {
        Scanner scanner = new Scanner(System.in);
        switch (selector) {
            case 0:
                // 退出
                return false;
            case 1:
                // 查看
                List<BusEntity> allBus = busMapper.getAllBus();
                Web.printfAllBus(allBus);
                return true;
            case 2:
                // 添加
                System.out.println("==========================================");
                System.out.println("开始添加班车");
                BusEntity insert = new BusEntity();
                System.out.println("请输入班车名称：");
                insert.setBusName(scanner.next().trim());
                System.out.println("请输入班车始发站：");
                insert.setDepartureStation(scanner.next().trim());
                System.out.println("请输入班车终点站：");
                insert.setTerminal(scanner.next().trim());
                boolean insertSuccess = busMapper.addBus(insert);
                if (insertSuccess) {
                    System.out.println("添加成功");
                } else {
                    System.out.println("添加失败");
                }
                return true;
            case 3:
                // 修改
                System.out.println("==========================================");
                System.out.println("开始修改班车");
                System.out.println("请输入修改班车编号");
                int updateId = scanner.nextInt();
                if (checkBusById(updateId) == false) {
                    return true;
                }
                System.out.println("请选择修改类型");
                System.out.println("1-修改班车名称");
                System.out.println("2-修改班车始发站");
                System.out.println("3-修改班车终点站");
                System.out.println("4-修改班车名称、始发站、终点站");
                BusEntity update = new BusEntity();
                update.setId(updateId);
                int updateSelect = scanner.nextInt();
                switch (updateSelect) {
                    case 1:
                        System.out.println("请输入班车名称：");
                        update.setBusName(scanner.next().trim());
                        break;
                    case 2:
                        System.out.println("请输入班车始发站：");
                        update.setDepartureStation(scanner.next().trim());
                        break;
                    case 3:
                        System.out.println("请输入班车终点站：");
                        update.setTerminal(scanner.next().trim());
                        break;
                    case 4:
                        System.out.println("请输入班车名称：");
                        update.setBusName(scanner.next().trim());
                        System.out.println("请输入班车始发站：");
                        update.setDepartureStation(scanner.next().trim());
                        System.out.println("请输入班车终点站：");
                        update.setTerminal(scanner.next().trim());
                        break;
                    default:
                }
                boolean updateSuccess = busMapper.updateBus(update, updateSelect);
                if (updateSuccess) {
                    System.out.println("修改成功");
                } else {
                    System.out.println("修改失败");
                }
                return true;
            case 4:
                // 删除
                System.out.println("==========================================");
                System.out.println("开始删除班车");
                System.out.println("请输入删除班车编号");
                int deleteId = scanner.nextInt();
                if (checkBusById(deleteId) == false) {
                    return true;
                }
                boolean deleteSuccess = busMapper.deleteBus(deleteId);
                if (deleteSuccess) {
                    System.out.println("删除成功");
                } else {
                    System.out.println("删除失败");
                }
                return true;
            default:
                Web.printfError(selector);
                return true;
        }
    }

    /**
     * 通过Id检查班车是否存在
     *
     * @param id
     * @return
     */
    private boolean checkBusById(int id) {
        List<BusEntity> busList = busMapper.getAllBy(id, null);
        if (null == busList || busList.size() <= 0) {
            System.out.println("该班车编号不存在：" + id);
            return false;
        }
        return true;
    }

    /**
     * 用户跟班车之间的操作
     *
     * @param selector
     * @return
     */
    public boolean operateUserBusMapping(int selector, int userId) {
        switch (selector) {
            case 0:
                // 退出
                return false;
            case 1:
                // 查看所有班车
                List<BusEntity> allBus = busMapper.getAllBus();
                Web.printfAllBus(allBus);
                return true;
            case 2:
                // 查看已预约的所有班车
                List<UserBusDto> allUserBus = userBusMappingMapper.getAllBus(userId);
                Web.printfAllUserBus(allUserBus);
                return true;
            case 3:
                // 预约-添加
                System.out.println("==========================================");
                System.out.println("开始预约班车");
                book(userId);
                return true;
            case 4:
                // 解约-删除
                System.out.println("==========================================");
                System.out.println("开始解约班车");
                cancel();
                return true;
            default:
                Web.printfError(selector);
                return true;
        }
    }

    /**
     * 预约
     *
     * @param userId
     * @return
     */
    private void book(int userId) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入班车编号");
        int busId = scanner.nextInt();
        if (checkBusById(busId) == false) {
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        System.out.println("请输入年份：");
        int year = scanner.nextInt();
        System.out.println("请输入月份：");
        int month = scanner.nextInt();
        System.out.println("请输入日份：");
        int day = scanner.nextInt();
        System.out.println("请输入时：");
        int hour = scanner.nextInt();
        System.out.println("请输入分：");
        int minute = scanner.nextInt();
        System.out.println("请输入秒：");
        int second = scanner.nextInt();
        stringBuilder.append(year).append(".").append(month).append(".").append(day).append(".")
                .append(hour).append(".").append(minute).append(".").append(second);
        if (CommonUtil.checkDate(year, month, day, hour, minute, second) == false) {
            System.out.println("您输入的时间格式不合法：" + CommonUtil.formatDate(stringBuilder.toString()));
            return;
        }
        UserBusMappingEntity userBusMappingEntity = new UserBusMappingEntity();
        userBusMappingEntity.setBusId(busId);
        userBusMappingEntity.setUserId(userId);
        userBusMappingEntity.setAppointmentTime(stringBuilder.toString());
        List<UserBusDto> allBus1 = userBusMappingMapper.getAllBus(userId);
        if (null != allBus1 && allBus1.size() > 0) {
            for (UserBusDto userBusDto : allBus1) {
                UserBusMappingEntity userBusMappingEntity1 = userBusDto.getUserBusMappingEntity();
                if (userBusMappingEntity1.getBusId() == userId && userBusMappingEntity1.getBusId() == busId
                        && userBusMappingEntity1.getAppointmentTime().equals(userBusMappingEntity.getAppointmentTime())) {
                    System.out.println("您已预约过时间：" + CommonUtil.formatDate(stringBuilder.toString() + " || 编号：" + busId + "的班车"));
                    return;
                }
            }
        }

        boolean insertSuccess = userBusMappingMapper.insert(userBusMappingEntity);
        if (insertSuccess) {
            System.out.println("预约成功");
        } else {
            System.out.println("预约失败");
        }
    }


    /**
     * 解约
     *
     * @return
     */
    private void cancel() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要解约的班车预约号：");
        int mappingNum = scanner.nextInt();
        List<UserBusDto> allBus = userBusMappingMapper.getById(mappingNum);
        if (null == allBus && allBus.size() <= 0) {
            System.out.println("您未预约当前班车");
            return;
        }
        boolean insertSuccess = userBusMappingMapper.delete(mappingNum);
        if (insertSuccess) {
            System.out.println("解约成功");
        } else {
            System.out.println("解约失败");
        }
    }
}
